package serialize;

import java.io.*;

/**
 * author : yeswater
 * date : 2023/7/4
 * <p>
 * Externalizable 实现序列化
 */

public class ExternalizeTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        test1();
    }

    private static void test1() throws IOException, ClassNotFoundException {
        User1 user = new User1();
        user.setUsername("yes");
        user.setId("water");
        ObjectOutput objectOutput = new ObjectOutputStream(new FileOutputStream(new File("user1")));
        objectOutput.writeObject(user);

        ObjectInput objectInput = new ObjectInputStream(new FileInputStream(new File("user1")));
        user = (User1) objectInput.readObject();

        System.out.println(user.getUsername());
        System.out.println(user.getId());

        objectOutput.close();
        objectInput.close();
    }

}
